/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package proyecto;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTable;

/**
 *
 * @author Luis
 */
public class consultar extends javax.swing.JFrame {
     Document documento= new Document ();
 String titulos[]={"idcliente","nombre","apellidop","apellidoM","FechaNa","Telefono","calle","colonia","Delegacion","cp","pesoinicial","pesofinal","unidad","inicial"};   

    /**
     * Creates new form consultar
     */
    public consultar() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jLabel1 = new javax.swing.JLabel();
        txtusuario = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txttabla = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Id Usuario");

        jButton1.setText("buscar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Guardar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Cancelar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        txttabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(txttabla);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(64, 64, 64)
                        .addComponent(jLabel1)
                        .addGap(43, 43, 43)
                        .addComponent(txtusuario, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(37, 37, 37)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1146, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(111, 111, 111)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(165, 165, 165)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(70, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtusuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(52, 52, 52)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(12, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
Consultor obj = new Consultor();
      obj.setVisible(true);
      dispose();
        
        
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
         
try{
    
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
            Connection cn=DriverManager.getConnection("jdbc:odbc:noko","Nokomotion","sasa");
            PreparedStatement pst=cn.prepareStatement("Select idcliente,Nombre ,ApellitoP,ApellidoM,FechaNa,Telefono,Callenumero,Colonia,Delegacion,Cp,Pesoinicial,Pesofinal,Unidad,Inicial from Clientes where idcliente=?");
            pst.setString(1,txtusuario.getText());
            ResultSet rs=pst.executeQuery();
            if(rs.next()){
              
                String datos[][]= new String [1][14];
                datos [0][0]=rs.getString("idcliente");
                datos [0][1]=rs.getString("Nombre");
                datos [0][2]=rs.getString("ApellitoP");
                datos [0][3]=rs.getString("ApellidoM");
                datos [0][4]=rs.getString("FechaNa");
                datos [0][5]=rs.getString("Telefono");
                datos [0][6]=rs.getString("Callenumero");
                datos [0][7]=rs.getString("Colonia");
                datos [0][8]=rs.getString("Delegacion");
                datos [0][9]=rs.getString("Cp");
                datos [0][10]=rs.getString("Pesoinicial");
                datos [0][11]=rs.getString("Pesofinal");
                datos [0][12]=rs.getString("Unidad");
                datos [0][13]=rs.getString("Inicial");
                txttabla=new JTable(datos,titulos);
                jScrollPane1.setViewportView(txttabla);
                }
             else{
                JOptionPane.showMessageDialog(null, "No existe dato");
             }
                }catch (Exception er){
            System.out.println("Error en buscar"+er);
  
}
        
        
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        
           try {
           JFileChooser  fileChooser = new JFileChooser();
         int result = fileChooser.showSaveDialog(null);
     String url = fileChooser.getSelectedFile().toString();
     
        FileOutputStream file = new FileOutputStream(new File(url+".pdf"));
             
      PdfWriter.getInstance(documento,file);
       documento.open();
       Paragraph parrafo = new Paragraph();
parrafo.setAlignment(Paragraph.ALIGN_BOTTOM);
parrafo.setFont(FontFactory.getFont("Sans",20,Font.BOLD, BaseColor.BLUE));
parrafo.add("DATOS DEL USUARIO \n\n");
documento.add(parrafo);

PdfPTable table = new PdfPTable(6);

try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
    Connection cn = DriverManager.getConnection("jdbc:odbc:noko","Nokomotion","sasa");
     PreparedStatement pst=cn.prepareStatement("Select idcliente,Nombre ,ApellitoP,ApellidoM,FechaNa,Telefono,Callenumero,Colonia,Delegacion,Cp,Pesoinicial,Pesofinal,Unidad,Inicial from Clientes where idcliente=?");
            pst.setString(1,txtusuario.getText());
            ResultSet rs=pst.executeQuery();
           
    if(rs.next())
    {
        do{
          
            table.addCell("Idusuario      ");
            table.addCell(rs.getString(1));
            table.addCell("Nombre         ");
            table.addCell(rs.getString(2));
            table.addCell("apellidoP      ");
            table.addCell(rs.getString(3));
            table.addCell("apellidoM      ");
            table.addCell(rs.getString(4));
            table.addCell("FechaNa        ");
            table.addCell(rs.getString(5));
            table.addCell("Telefono       ");
            table.addCell(rs.getString(6));
            table.addCell("Callenumero    ");
            table.addCell(rs.getString(7));
            table.addCell("Colonia        ");
            table.addCell(rs.getString(8));
            table.addCell("Delegacion     ");
            table.addCell(rs.getString(9));
            table.addCell("Cp             ");
            table.addCell(rs.getString(10));
            table.addCell("Pesoinicial    ");
            table.addCell(rs.getString(11));
            table.addCell("Pesofinal      ");
            table.addCell(rs.getString(12));
            table.addCell("Unidad         ");
            table.addCell(rs.getString(13));
            table.addCell("Inicial        ");
            table.addCell(rs.getString(14));
 
        }while(rs.next());
        documento.add(table);
    }

}catch (Exception er){
}

documento.close();

 JOptionPane.showMessageDialog(null, "Se ha Guardado Exitosamente");

    }catch(Exception e){
        System.out.println("Error"+e);
    }
  // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(consultar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(consultar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(consultar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(consultar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new consultar().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable txttabla;
    private javax.swing.JTextField txtusuario;
    // End of variables declaration//GEN-END:variables
}
